////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Roman Ranzmaier (http://www.ranzmaier.at/contact.aspx).  All rights reserved.
// 
// Codeplex project: http://gadgeteernetworking.codeplex.com
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using Microsoft.SPOT;
using System.Collections;
using System.Net;

namespace Ranzmaier.NETMF.GadgeteerNetworking
{
    public static class NetworkModuleExtensions
    {

        private static NetWorkServerBase CreateServer(this Gadgeteer.Modules.Module.NetworkModule module,
               NetworkServerProtocol protocol,
               int port,
               int numOfListener = 1,
               int timeout = 30000)
        {
            NetWorkServerBase s = null;
            if (protocol == NetworkServerProtocol.TCP)
                s = new TcpServer(IPAddress.Parse(module.NetworkSettings.IPAddress), port, numOfListener, timeout);
            if (protocol == NetworkServerProtocol.UDP)
                s = new UdpServer(port, numOfListener, timeout);
            return s;
        }


        /// <summary>
        /// Creates a new TCP server.
        /// </summary>
        /// <param name="module">The gadgeteer NetworkModule.</param>
        /// <param name="port">The serverport.</param>
        /// <param name="timeout">Sockettimeout .</param>
        /// <returns>
        /// a new instance of <see cref="TcpServer"/>
        /// </returns>
        /// <exception cref="ArgumentException">Port already in use</exception>
        public static TcpServer CreateTCPServer(this Gadgeteer.Modules.Module.NetworkModule module,int port,
            int timeout = 30000)
        {
            return (TcpServer)module.CreateServer(NetworkServerProtocol.TCP, port, 1, timeout);

        }
        /// <summary>
        /// Creates a new UDP server.
        /// </summary>
        /// <param name="module">The gadgeteer NetworkModule.</param>
        /// <param name="port">The serverport.</param>
        /// <param name="timeout">Sockettimeout .</param>
        /// <returns>
        /// a new instance of <see cref="UdpServer"/>
        /// </returns>
        /// <exception cref="ArgumentException">Port already in use</exception>
        public static UdpServer CreateUDPServer(this Gadgeteer.Modules.Module.NetworkModule module,int port,
            int timeout = 30000)
        {
            return (UdpServer)module.CreateServer(NetworkServerProtocol.UDP, port, 1, timeout);

        }
    }
}
